package com.xj.zhihudemo.ui.adapter;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xj.zhihudemo.R;
import com.xj.zhihudemo.mode.MenuInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Xujing on 16/8/31.
 */
public class MenuAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int TYPE_HEADER = 0;
    public static final int TYPE_NORMAL = 1;
    private View mHeaderView;
    private View mFooterView;
    private List<MenuInfo.OthersEntity> mDatas = new ArrayList<MenuInfo.OthersEntity>();
    private Context mContext;
    private LayoutInflater mInflater;
    private OnItemClickListener mOnItemClickListener;

    public MenuAdapter(Context context) {
        this.mContext = context;
        mInflater = LayoutInflater.from(mContext);

    }

    @Override
    public int getItemViewType(int position) {
        if (mHeaderView == null) return TYPE_NORMAL;
        if (position == 0) return TYPE_HEADER;
        return TYPE_NORMAL;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mHeaderView != null && viewType == TYPE_HEADER) return new Holder(mHeaderView);
        View layout = mInflater.inflate(R.layout.item_menu, parent, false);
        return new Holder(layout);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_HEADER) return;
        final int pos = getRealPosition(holder);
        final MenuInfo.OthersEntity data = mDatas.get(pos);
        if (holder instanceof Holder) {
            ((Holder) holder).text.setText(data.getName());
            if (mOnItemClickListener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mOnItemClickListener.onItemClick(view, pos, data);

                    }
                });
                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View view) {
                        mOnItemClickListener.onItemLongClick(view, pos, data);
                        return false;
                    }
                });
            }
        }
    }

    public int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return mHeaderView == null ? position : position - 1;
    }


    @Override
    public int getItemCount() {
        return mHeaderView == null ? mDatas.size() : mDatas.size() + 1;
    }


    public void addHeaderView(View mHeaderView) {
        this.mHeaderView = mHeaderView;

    }


    public void addFooterView(View mFooterView) {
        this.mFooterView = mFooterView;

    }


    public void addData(List<MenuInfo.OthersEntity> mList) {
        mDatas.clear();
        mDatas.addAll(mList);
        notifyDataSetChanged();
    }


    public interface OnItemClickListener {
        void onItemClick(View view, int position, MenuInfo.OthersEntity data);
        void onItemLongClick(View view, int position, MenuInfo.OthersEntity data);
    }

    class Holder extends RecyclerView.ViewHolder {
        TextView text;

        public Holder(View itemView) {
            super(itemView);
            if (itemView == mHeaderView) return;
            text = (TextView) itemView.findViewById(R.id.menu_item);
        }
    }
}

